package hutech.ksv.service;

import hutech.ksv.common.KenhSinhVienContants;
import hutech.ksv.dao.UserAccountDao;
import hutech.ksv.entity.ChuNhaTroEntity;
import hutech.ksv.entity.ChuQuanAnEntity;
import hutech.ksv.entity.CongTyTuyenDungEntity;
import hutech.ksv.entity.SinhVienEntity;
import hutech.ksv.entity.TaiKhoanCNTENtity;
import hutech.ksv.entity.TaiKhoanCQAEntity;
import hutech.ksv.entity.TaiKhoanCTEntity;
import hutech.ksv.entity.TaiKhoanSVEntity;
import hutech.ksv.entity.UserAccountHashtable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserAccountService implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	@Autowired
	UserAccountDao userAccountDao;
	
	@Autowired
	EncryptMD5 encryptMD5;
	
	public UserAccountHashtable checkLogin(String username, String password) {
		UserAccountHashtable userAccountHashtable = new UserAccountHashtable();
		
		//ma hoa password
		password = encryptMD5.md5(password);
		
		List<TaiKhoanSVEntity> listTaiKhoanSV = userAccountDao.getTaiKhoanSV();
		for (TaiKhoanSVEntity taiKhoanSV : listTaiKhoanSV) {
			if( taiKhoanSV.getTenTKSV().equals(username)
					&& taiKhoanSV.getMatKhauTKSV().equals(password)) {
				userAccountHashtable.setKey(KenhSinhVienContants.TAI_KHOAN_SINH_VIEN);
				userAccountHashtable.setValue(taiKhoanSV);
				return userAccountHashtable;
			}
		}
		
		List<TaiKhoanCQAEntity> listTaiKhoanCQA = userAccountDao.getTaiKhoanCQA();
		for (TaiKhoanCQAEntity taiKhoanCQA : listTaiKhoanCQA) {
			if( taiKhoanCQA.getTenTKCQA().equals(username)
					&& taiKhoanCQA.getMatKhauCQA().equals(password)) {
				userAccountHashtable.setKey(KenhSinhVienContants.TAI_KHOAN_CHU_QUAN_AN);
				userAccountHashtable.setValue(taiKhoanCQA);
				return userAccountHashtable;
			}
		}
		
		List<TaiKhoanCTEntity> listTaiKhoanCT = userAccountDao.getTaiKhoanCT();
		for (TaiKhoanCTEntity taiKhoanCT : listTaiKhoanCT) {
			if( taiKhoanCT.getTenTKCT().equals(username)
					&& taiKhoanCT.getMatKhauCT().equals(password)) {
				userAccountHashtable.setKey(KenhSinhVienContants.TAI_KHOAN_CONG_TY);
				userAccountHashtable.setValue(taiKhoanCT);
				return userAccountHashtable;
			}
		}
		
		List<TaiKhoanCNTENtity> listTaiKhoanCNT = userAccountDao.getTaiKhoanCNT();
		for (TaiKhoanCNTENtity taiKhoanCNT : listTaiKhoanCNT) {
			if( taiKhoanCNT.getTenTKCNT().equals(username)
					&& taiKhoanCNT.getMatKhauCNT().equals(password)) {
				userAccountHashtable.setKey(KenhSinhVienContants.TAI_KHOAN_CHU_NHA_TRO);
				userAccountHashtable.setValue(taiKhoanCNT);
				return userAccountHashtable;
			}
		}
		return null;
	}

	//Them thong tin
	public int themSinhVien(SinhVienEntity sinhVienEntity) {
		return userAccountDao.themSinhVien(sinhVienEntity);
	}
	
	public int themChuNhaTro(ChuNhaTroEntity chuNhaTroEntity) {
		return userAccountDao.themChuNhaTro(chuNhaTroEntity);
	}
	
	public int themChuQuanAn(ChuQuanAnEntity chuQuanAnEntity) {
		return userAccountDao.themChuQuanAn(chuQuanAnEntity);
	}
	
	public int themCongTyTuyenDung(CongTyTuyenDungEntity congTyEntity) {
		return userAccountDao.themCongTyTuyenDung(congTyEntity);
	}
	//Ket thuc them thong tin
	
	//Them tai khoan
	public int themTaiKhoanSV(TaiKhoanSVEntity taiKhoanSVEntity) {
		return userAccountDao.themTaiKhoanSV(taiKhoanSVEntity);
	}
	
	public int themTaiKhoanCNT(TaiKhoanCNTENtity taiKhoanCNT) {
		return userAccountDao.themTaiKhoanCNT(taiKhoanCNT);
	}
	
	public int themTaiKhoanCQA(TaiKhoanCQAEntity taiKhoanCQA) {
		return userAccountDao.themTaiKhoanCQA(taiKhoanCQA);
	}
	
	public int themTaiKhoanCT(TaiKhoanCTEntity taiKhoanCT) {
		return userAccountDao.themTaiKhoanCongTy(taiKhoanCT);
	}
	//Ket thuc them tai khoan
	
	//Kiem tra tai khoan da ton tai chua
	public ArrayList<String> getTatCaTaiKhoanUser() {	
		ArrayList<String> result = new ArrayList<String>();
		
		List<TaiKhoanSVEntity> listTaiKhoanSV = userAccountDao.getTaiKhoanSV();
		for (TaiKhoanSVEntity taiKhoanSV : listTaiKhoanSV) {
			result.add(taiKhoanSV.getTenTKSV());
		}
		
		List<TaiKhoanCQAEntity> listTaiKhoanCQA = userAccountDao.getTaiKhoanCQA();
		for (TaiKhoanCQAEntity taiKhoanCQA : listTaiKhoanCQA) {
			result.add(taiKhoanCQA.getTenTKCQA());
		}
		
		List<TaiKhoanCTEntity> listTaiKhoanCT = userAccountDao.getTaiKhoanCT();
		for (TaiKhoanCTEntity taiKhoanCT : listTaiKhoanCT) {
			result.add(taiKhoanCT.getTenTKCT());
		}
		
		List<TaiKhoanCNTENtity> listTaiKhoanCNT = userAccountDao.getTaiKhoanCNT();
		for (TaiKhoanCNTENtity taiKhoanCNT : listTaiKhoanCNT) {
			result.add(taiKhoanCNT.getTenTKCNT());
		}
		
		return result;
	}
	
	public boolean kiemTraTaiKhoanDaTonTai(String tenTaiKhoan) {
		ArrayList<String> danhSachTenTaiKhoan = getTatCaTaiKhoanUser();
		
		for(int i = 0; i < danhSachTenTaiKhoan.size(); i++) {
			if(tenTaiKhoan.equals(danhSachTenTaiKhoan.get(i))) {
				return true;
			}
		}		
		return false;
	}	
	//ket thuc kiem tra tai khoan
	
	//Xoa tai khoan vua moi nhap khi tai khoan bi trung
	public int xoaTaiKhoanSVVuaNhap(int maSV) {
		return userAccountDao.xoaThongTinSVVuaNhap(maSV);
	}
	
	public int xoaTaiKhoanCNTVuaNhap(int maCNT) {
		return userAccountDao.xoaThongTinCNTVuaNhap(maCNT);
	}
	
	public int xoaTaiKhoanCQAVuaNhap(int maCQA) {
		return userAccountDao.xoaThongTinCQAVuaNhap(maCQA);
	}
	
	public int xoaTaiKhoanCTVuaNhap(int maCT) {
		return userAccountDao.xoaThongTinCTVuaNhap(maCT);
	}
	//ket thuc xoa tai khoan
}
